using System;
using System.Web.Security;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using RMH.DataAccess;
using RMH.DataAccess.AdoNet;
using RMH.Entities;
using RMH.Services;
using RMH.Web;

namespace RMH.IntegrationTests.Repository
{
    [TestFixture, Explicit]
    public class CreateSchema
    {


        [Test]        
        public void LoadData()
        {
            create_schema();
            load_counties();
            load_rooms();
            load_diagnosis();
            load_fake_patients();
            load_fake_support_person();
            load_fake_patients_into_rooms();
            load_relationships_for_chuck_norris();
            load_staff_members();
            create_schema_ado();
        }
        
        public void create_schema()
        {
            var factory = new SessionFactoryFactory();
            factory.OnFactoryCreation = configuration =>
            {
                var exporter = new SchemaExport(configuration);
                exporter.Create(true, true);
                exporter.Execute(false, true, false, true);
                Console.WriteLine("done");
            };
            ISessionFactory sessionFactory = factory.GetSessionFactory();
        }


        public void create_schema_ado()
        {
            // TODO: fix this. 
            //AdoDataHelper.ExecuteScript("ReportEntities.sql");
            //AdoDataHelper.ExecuteScript("ReportEntities.sql");
            //AdoDataHelper.ExecuteScript("DDL-Update.sql");            
        }
        
        private void load_relationships_for_chuck_norris()
        {
            var patientService = Container.Resolve<IPatientService>();
            var personService = Container.Resolve<ISupportPersonService>();
            var diagnosisService = Container.Resolve<IDiagnosisService>();

            var chucks = patientService.Search("Chuck", "Nor");
            foreach (var chuck in chucks)
            {
               
      
                    chuck.Diagnoses.Add(diagnosisService.Get(1));
                    chuck.Diagnoses.Add(diagnosisService.Get(2));
                    chuck.Diagnoses.Add(diagnosisService.Get(3));
               
                patientService.Save(chuck);
            }                            
        }

        private void load_fake_support_person()
        {
            var repository = Container.Resolve<ISupportPersonRepository>();
            repository.Save(new SupportPerson { FirstName = "Aunt", LastName = "May", PhoneNumber = "(555) 555-5309" });
            repository.Save(new SupportPerson { FirstName = "Archie", LastName = "Bunker", PhoneNumber = "(555) 555-3423" });
            repository.Save(new SupportPerson { FirstName = "Cindy", LastName = "Norris", PhoneNumber = "(212) 867-5309" });
        }

        private void load_diagnosis()
        {
            var repository = Container.Resolve<DiagnosisRepository>();
            repository.Save(new Diagnosis { Name = "Anemia" });
            repository.Save(new Diagnosis { Name = "Autism" });
            repository.Save(new Diagnosis { Name = "Cancer" });
            repository.Save(new Diagnosis { Name = "Celiac Disease" });
            repository.Save(new Diagnosis { Name = "Crohns Disease" });
            repository.Save(new Diagnosis { Name = "Cystic Fibrosis" });
            repository.Save(new Diagnosis { Name = "Diabetes" });
            repository.Save(new Diagnosis { Name = "Lukemia" });
        }

        private void load_staff_members()
        {
            if (Membership.GetUser("givecamp") == null) 
                Membership.CreateUser("givecamp", "lansing09", "info@lansinggivecamp.org");
            if (Membership.GetUser("givecampvol") == null) Membership.CreateUser("givecampvol", "lansing09", "infovol@lansinggivecamp.org");
            if (!Roles.RoleExists("Administrator")) Roles.CreateRole("Administrator");
            if (!Roles.RoleExists("Volunteer")) Roles.CreateRole("Volunteer");
            if (!Roles.IsUserInRole("givecamp", "Administrator")) Roles.AddUserToRole("givecamp", "Administrator");
            if (!Roles.IsUserInRole("givecampvol", "Volunteer")) Roles.AddUserToRole("givecampvol", "Volunteer");
        }

        private void load_rooms()
        {
            var repository = Container.Resolve<RoomRepository>();
            repository.Save(new Room {RoomNumber = 1});
            repository.Save(new Room {RoomNumber = 2});
            repository.Save(new Room {RoomNumber = 3});
            repository.Save(new Room {RoomNumber = 4});
            repository.Save(new Room {RoomNumber = 5});
            repository.Save(new Room {RoomNumber = 6});
            repository.Save(new Room {RoomNumber = 7});
            repository.Save(new Room {RoomNumber = 8});
            repository.Flush();
        }


        public void load_fake_patients()
        {
            var patient = new Patient {FirstName = "Carlos", MiddleName = "Ray", LastName = "Norris", EmailAddress = "chuck@hello.com", DateOfBirth = new DateTime(1940, 3, 10)};
            var patient2 = new Patient { FirstName = "Job", MiddleName = "A", LastName = "Bluth", EmailAddress = "job@email.com", DateOfBirth = new DateTime(1975, 2, 11) };
            var patient3 = new Patient { FirstName = "Michael", MiddleName = "B", LastName = "Bluth", EmailAddress = "micahel@email.com", DateOfBirth = new DateTime(1978, 1, 14) };
            var patient4 = new Patient { FirstName = "Homer", MiddleName = "J", LastName = "Simpson", EmailAddress = "homer@email.com", DateOfBirth = new DateTime(1985, 7, 23) };
            var patient5 = new Patient { FirstName = "Tom", MiddleName = "J", LastName = "Izzo", EmailAddress = "izzo@email.com", DateOfBirth = new DateTime(1955, 8, 4) };
            var patient6 = new Patient { FirstName = "Chuck", MiddleName = "I", LastName = "Norris", EmailAddress = "billy@email.com", DateOfBirth = new DateTime(1955, 4, 8), DateOfDeath = new DateTime(2009, 3, 20) };
            var patient7 = new Patient { FirstName = "Chuck", MiddleName = "Doe", 
                LastName = "Norris", EmailAddress = "chuck@heeeeeeeel.com", 
                DateOfBirth = new DateTime(1930, 1, 9), 
                DateOfDeath = new DateTime(2009, 4, 21),
                Address = new Address
                              {
                                  City = "Hunlock Creek",
                                  PostalCode = "19103",
                                  State = "PA",
                                  Street1 = "Box 448",
                                  Street2 = "SorbertownHill"
                          }, PhoneNumber = "232-232-2323", 
                VehicleMake = "Ford",
                VehicleModel = "Temp",
                VehiclePlateNumber = "ILOVETOFU",
                VehiclePlateState = "MI"};
            var patient8 = new Patient { FirstName = "Carl", MiddleName = "Woooooo", 
                LastName = "Deceased", EmailAddress = "billy@email.com", DateOfBirth = new DateTime(1922, 5, 10), DateOfDeath = new DateTime(2009, 4, 18) };
            
            var patientRepository = Container.Resolve<PatientRepository>();

            patientRepository.Save(patient);
            patientRepository.Save(patient2);
            patientRepository.Save(patient3);
            patientRepository.Save(patient4);
            patientRepository.Save(patient5);
            patientRepository.Save(patient6);
            patientRepository.Save(patient7);
            patientRepository.Save(patient8);
        }

        private void load_fake_patients_into_rooms()
        {            
           // AdoDataHelper.ExecuteScript("FakeStayData.sql");
        }

        public void load_counties()
        {
            var countyRepository = Container.Resolve<CountyRepository>();

            countyRepository.Save(new County {Name = "-Out of State-"});
            countyRepository.Save(new County {Name = "Alcona"});
            countyRepository.Save(new County {Name = "Alger"});
            countyRepository.Save(new County {Name = "Allegan"});
            countyRepository.Save(new County {Name = "Alpena"});
            countyRepository.Save(new County {Name = "Antrim"});
            countyRepository.Save(new County {Name = "Arenac"});
            countyRepository.Save(new County {Name = "Baraga"});
            countyRepository.Save(new County {Name = "Barry"});
            countyRepository.Save(new County {Name = "Bay"});
            countyRepository.Save(new County {Name = "Benzie"});
            countyRepository.Save(new County {Name = "Berrien"});
            countyRepository.Save(new County {Name = "Branch"});
            countyRepository.Save(new County {Name = "Calhoun"});
            countyRepository.Save(new County {Name = "Cass"});
            countyRepository.Save(new County {Name = "Charlevoix"});
            countyRepository.Save(new County {Name = "Cheboygan"});
            countyRepository.Save(new County {Name = "Chippewa"});
            countyRepository.Save(new County {Name = "Clare"});
            countyRepository.Save(new County {Name = "Clinton"});
            countyRepository.Save(new County {Name = "Crawford"});
            countyRepository.Save(new County {Name = "Delta"});
            countyRepository.Save(new County {Name = "Dickinson"});
            countyRepository.Save(new County {Name = "Eaton"});
            countyRepository.Save(new County {Name = "Emmet"});
            countyRepository.Save(new County {Name = "Genesee"});
            countyRepository.Save(new County {Name = "Gladwin"});
            countyRepository.Save(new County {Name = "Gogebic"});
            countyRepository.Save(new County {Name = "Grand Traverse"});
            countyRepository.Save(new County {Name = "Gratiot"});
            countyRepository.Save(new County {Name = "Hillsdale"});
            countyRepository.Save(new County {Name = "Houghton"});
            countyRepository.Save(new County {Name = "Huron"});
            countyRepository.Save(new County {Name = "Ingham"});
            countyRepository.Save(new County {Name = "Ionia"});
            countyRepository.Save(new County {Name = "Iosco"});
            countyRepository.Save(new County {Name = "Iron"});
            countyRepository.Save(new County {Name = "Isabella"});
            countyRepository.Save(new County {Name = "Jackson"});
            countyRepository.Save(new County {Name = "Kalamazoo"});
            countyRepository.Save(new County {Name = "Kalkaska"});
            countyRepository.Save(new County {Name = "Kent"});
            countyRepository.Save(new County {Name = "Keweenaw"});
            countyRepository.Save(new County {Name = "Lake"});
            countyRepository.Save(new County {Name = "Lapeer"});
            countyRepository.Save(new County {Name = "Leelanau"});
            countyRepository.Save(new County {Name = "Lenawee"});
            countyRepository.Save(new County {Name = "Livingston"});
            countyRepository.Save(new County {Name = "Luce"});
            countyRepository.Save(new County {Name = "Mackinac"});
            countyRepository.Save(new County {Name = "Macomb"});
            countyRepository.Save(new County {Name = "Manistee"});
            countyRepository.Save(new County {Name = "Marquette"});
            countyRepository.Save(new County {Name = "Mason"});
            countyRepository.Save(new County {Name = "Mecosta"});
            countyRepository.Save(new County {Name = "Menominee"});
            countyRepository.Save(new County {Name = "Midland"});
            countyRepository.Save(new County {Name = "Missaukee"});
            countyRepository.Save(new County {Name = "Monroe"});
            countyRepository.Save(new County {Name = "Montcalm"});
            countyRepository.Save(new County {Name = "Montmorency"});
            countyRepository.Save(new County {Name = "Muskegon"});
            countyRepository.Save(new County {Name = "Newaygo"});
            countyRepository.Save(new County {Name = "Oakland"});
            countyRepository.Save(new County {Name = "Oceana"});
            countyRepository.Save(new County {Name = "Ogemaw"});
            countyRepository.Save(new County {Name = "Ontonagon"});
            countyRepository.Save(new County {Name = "Osceola"});
            countyRepository.Save(new County {Name = "Oscoda"});
            countyRepository.Save(new County {Name = "Otsego"});
            countyRepository.Save(new County {Name = "Ottawa"});
            countyRepository.Save(new County {Name = "Presque Isle"});
            countyRepository.Save(new County {Name = "Roscommon"});
            countyRepository.Save(new County {Name = "Saginaw"});
            countyRepository.Save(new County {Name = "Sanilac"});
            countyRepository.Save(new County {Name = "Schoolcraft"});
            countyRepository.Save(new County {Name = "Shiawassee"});
            countyRepository.Save(new County {Name = "St. Clair"});
            countyRepository.Save(new County {Name = "St. Joseph"});
            countyRepository.Save(new County {Name = "Tuscola"});
            countyRepository.Save(new County {Name = "Van Buren"});
            countyRepository.Save(new County {Name = "Washtenaw"});
            countyRepository.Save(new County {Name = "Wayne"});
            countyRepository.Save(new County {Name = "Wexford"});
            countyRepository.Flush();
        }
    }
}